package cn.edu.sasu.service.Impl;

import cn.edu.sasu.common.base.LayuiRespResult;
import cn.edu.sasu.common.base.RespResult;
import cn.edu.sasu.common.constants.Constant;
import cn.edu.sasu.common.util.DateUtils;
import cn.edu.sasu.domain.dto.ChangDocDTO;
import cn.edu.sasu.domain.dto.PatientSearchDTO;
import cn.edu.sasu.domain.dto.SysRoleSaveDTO;
import cn.edu.sasu.domain.entity.Doctor;
import cn.edu.sasu.domain.entity.Patient;
import cn.edu.sasu.domain.entity.PatientEducationActivity;
import cn.edu.sasu.domain.vo.AdressAndCountVO;
import cn.edu.sasu.domain.vo.PatientVO;
import cn.edu.sasu.domain.vo.SysRoleVO;
import cn.edu.sasu.domain.vo.SysUserVO;
import cn.edu.sasu.mapper.PatientMapper;
import cn.edu.sasu.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 功能：
 * 作者：zwf
 * 日期：2024/4/27 14:44
 */
@Service
public class PatientServiceImpl implements PatientService {

    @Autowired
    private PatientMapper patientMapper;

    /**
     * 新增患者
     * @param patient
     * @return
     */
    @Override
    public RespResult save(Patient patient) {
        //查询该用户是否已经添加
        Patient patient1  = patientMapper.findPatientByName(patient.getPatientName());
        if(patient1!=null){
            return RespResult.error("该用户已存在");
        }
        patient.setCreateTime(DateUtils.getCurrentDate());
        if(patientMapper.save(patient)<=0){
            return RespResult.error("添加失败");
        }
        return RespResult.success("添加成功");
    }

    /**
     * 查询患者信息
     * @param page
     * @param limit
     * @return
     */
    @Override
    public LayuiRespResult findPatientAll(Integer page, Integer limit) {
        Integer offset = (page - 1) * limit;
        Integer rows = limit;
        List<PatientVO> sysRoles = patientMapper.findPatientList(Constant.DELETE_FLAG_YES,offset,rows);
        Integer totalSize = patientMapper.countPatient(Constant.DELETE_FLAG_YES);
        return LayuiRespResult.success(totalSize,sysRoles);
    }

    /**
     * 批量删除患者
     * @param userIds
     * @return
     */
    @Override
    public RespResult batchdel(Integer[] userIds) {
        patientMapper.updatePatientDeleteFlagByUserIds(Arrays.asList(userIds),Constant.DELETE_FLAG_YES);
        return RespResult.success("删除成功！");
    }

    /**
     * 查询所有医生信息
     * @return
     */
    @Override
    public RespResult getDoctor() {
        List<Doctor> doctors = patientMapper.getDoctor();
        if(doctors!=null&&doctors.size()>0){
            return RespResult.success(doctors);
        }
        return RespResult.error("查询失败");
    }

    /**
     * 更新患者信息
     * @param patient
     * @return
     */
    @Override
    public RespResult update(PatientVO patient) {
        Integer update  = patientMapper.update(patient);
        if(update>0){
            return RespResult.success("更新成功");
        }
        return RespResult.error("更新失败");
    }

    /**
     * 更换医生
     * @param changDocDTO
     * @return
     */
    @Override
    public RespResult changD(ChangDocDTO changDocDTO) {
        Integer update  = patientMapper.changD(changDocDTO);
        if(update>0){
            return RespResult.success("更换成功");
        }
        return RespResult.error("更换成功");
    }

    /**
     * 搜索患者
     * @param patientSearchDTO
     * @return
     */
    @Override
    public LayuiRespResult search(PatientSearchDTO patientSearchDTO) {
        Integer offset = (patientSearchDTO.getPage() - 1) * patientSearchDTO.getLimit();
        Integer rows = patientSearchDTO.getLimit();
        List<PatientVO> patients = patientMapper.searchPatientList(Constant.DELETE_FLAG_YES,offset,rows,patientSearchDTO);
        Integer totalSize = patientMapper.countSearchPatient(Constant.DELETE_FLAG_YES,patientSearchDTO);
        return LayuiRespResult.success(totalSize,patients);
    }

    /**
     * SELECT patient_address, COUNT(*) AS COUNT
     * FROM patient
     * GROUP BY patient_address;
     * @return
     */
    @Override
    public RespResult getCount() {
        List<AdressAndCountVO> list = patientMapper.getCount();
        if(list!=null&&list.size()>0){
            return RespResult.success(list);
        }
        return RespResult.error("查询失败");
    }

    @Override
    public LayuiRespResult activityListPatient(Integer page, Integer limit, Integer doctorId) {
        Integer offset = (page - 1) * limit;
        Integer rows = limit;
        List<PatientVO> acDepts = patientMapper.activityListPatient(Constant.DELETE_FLAG_YES,offset,rows,doctorId);
        Integer totalSize = patientMapper.countActivitiesPatient(Constant.DELETE_FLAG_YES,doctorId);
        return LayuiRespResult.success(totalSize,acDepts);
    }

    @Override
    public List<Patient> findAll() {
        List<Patient> patients = patientMapper.findAll();
        return patients;
    }
}
